//
//  DefineFlowLayout.m
//  TheCollectionView
//
//  Created by ODSdev2 on 15/9/16.
//  Copyright (c) 2015年 liubenwang. All rights reserved.
//

#import "DefineFlowLayout.h"

@interface DefineFlowLayout()
//记录每行的高度
@property (nonatomic, strong) NSMutableDictionary * heightDic;

@property (nonatomic, strong) NSMutableArray * attriArray;/**<记录所有属性*/
@end
@implementation DefineFlowLayout
@synthesize sectionInset = _sectionInset;
//懒加载
- (NSMutableDictionary *)heightDic{

    if (!_heightDic) {
        _heightDic = [[NSMutableDictionary alloc]init];
    }
    return _heightDic;
}
- (NSMutableArray *)attriArray{

    if (!_attriArray) {
        _attriArray = [[NSMutableArray alloc]init];
    }
    return _attriArray;
}
//是否允许重新布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{

    return YES;
}
//每次布局均调用
- (void)prepareLayout{

    [super prepareLayout];
    
    //初始化
    for (int i = 0; i< _columnsCount; i++) {
        [self.heightDic setObject:@(_sectionInset.top) forKey:[NSString stringWithFormat:@"%d",i]];
    }
    
    [self.attriArray removeAllObjects];
    NSInteger counts = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i< counts; i++) {
        
        UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
        [_attriArray addObject:attributes];
    }
}
- (void)prepareForCollectionViewUpdates:(NSArray *)updateItems{


}




//返回所有属性
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{

    __block NSString * minColumns = @"0";
    //查找最短的列
    [_heightDic enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber * obj, BOOL *stop) {
       
        if ([obj floatValue]<[_heightDic[minColumns] floatValue]) {
            minColumns = key;
        }
    }];
    
    CGFloat width = (self.collectionView.frame.size.width-_sectionInset.left-_sectionInset.right-(_columnsCount-1)*_columnsMargin)/_columnsCount;
    CGFloat height = [self.delegate defineFlowLayout:self andWith:width atIndexth:indexPath];
    CGFloat x = _sectionInset.left+(width+_columnsMargin)*[minColumns intValue];
    
    CGFloat y = [_heightDic[minColumns] floatValue]+_sectionInset.top;
    
    _heightDic[minColumns] = @(y+height);
    
    UICollectionViewLayoutAttributes * attribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    attribute.frame = CGRectMake(x, y, width, height);
    
    return attribute;
}
//返回滑动尺寸
- (CGSize)collectionViewContentSize{

    __block NSString * maxColumns = @"0";
    
    [_heightDic enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSNumber * obj, BOOL *stop) {
        
        if ([obj floatValue]>[_heightDic[maxColumns] floatValue]) {
            maxColumns = key;
        }
    }];
//1553164986
//485870606
    return CGSizeMake(0, [_heightDic[maxColumns] floatValue]+self.sectionInset.bottom);
}
//返还所有属性
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{

    return _attriArray;
}

@end
